package client.superadmin;

import static org.stjs.javascript.JSCollections.$array;
import static org.stjs.javascript.JSCollections.$map;

import org.stjs.javascript.Array;
import org.stjs.javascript.Map;

import client.common.EndPoints.SupadminEP;
import client.common.Models.Materia;
import client.common.angular.NgScope;
import client.common.angular.UIState;
import client.common.directives.EPModal;
import client.common.javascript.JsProcedure1;

public class ColMateriasScope extends NgScope {

	public static Array<String> $inject() {
		return $array("$stateParams", "Superadmin", "epModal", "$state");
	}

	private Long idColegio;
	private Array<Materia> materias;
	private Materia nuevaMateria;
	private SupadminEP superEP;
	private EPModal epModal;
	private UIState state;

	public ColMateriasScope(Map<String, Long> stateParams, SupadminEP superEP,
			EPModal epModal, UIState state) {

		super("ColMateriasScope");
		this.state = state;
		idColegio = stateParams.$get("idColegio");
		materias = superEP.getMateriasColegio(idColegio);
		this.superEP = superEP;
		this.epModal = epModal;

	}

	public void verMateria(final Materia m) {
		state.transitionTo("colegio.ofertaAcademica.materia",
				$map("idMateria", m.id, "idColegio", idColegio));
	}

	public void crearMateria() {
		final ColMateriasScope self = this;
		nuevaMateria.idColegio = idColegio;
		superEP.newMateriaColegio(nuevaMateria, new JsProcedure1<Materia>() {

			@Override
			public void $invoke(Materia p) {

				self.materias.push(p);
				self.epModal.close("nueva-materia");
				self.nuevaMateria = new Materia();
			}
		});
	}
}
